package com.example.springboot.utils;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.example.springboot.entity.Patent;
import com.huaban.analysis.jieba.JiebaSegmenter;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.FileCopyUtils;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.*;

public class JieBaUtil {

    //分词、去停用词
    public List<String> separateAndClear(String input) throws IOException {
        JiebaSegmenter segment = new JiebaSegmenter();
        //获取停用词
        ClassPathResource resource = new ClassPathResource("static\\stopWords.txt");

        byte[]  keywordsData = FileCopyUtils.copyToByteArray(resource.getInputStream());

        String[] strs = new String(keywordsData, StandardCharsets.UTF_8).split("\r\n");

        List<String> stopWords = Arrays.asList(strs);
        //创建结果列表 分词
        List<String> output = new ArrayList<>(segment.sentenceProcess(input));
        //去除停用词
        output.removeAll(stopWords);
        //去重
        HashSet<String> hashSet = new HashSet<>(output);
        List<String> result = new ArrayList<>(hashSet);
        result.removeIf(StrUtil::isBlank);
        System.out.println(result);
        return result;
    }

    //对传入的两个专利列表进行合并
    public List<Patent> listsToOne(List<Patent> result,List<Patent> perResult) {
        if (!result.isEmpty()) {
            for (Patent patent1 : result) {
                for (Iterator<Patent> it = perResult.iterator(); it.hasNext(); ) {
                    Patent patent2 = it.next();
                    if (patent1.getGkh().equals(patent2.getGkh())) {
                        patent1.setWeight(patent1.getWeight() + patent2.getWeight());
                        it.remove();
                    }
                }
            }
        }
        result.addAll(perResult);
        return result;
    }

//    public static void main(String[] args) throws IOException {
//        JieBaUtil jieBaUtil = new JieBaUtil();
//        jieBaUtil.separateAndClear("");
//    }


}
